package com.sticktogether;

import java.util.ArrayList;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ImageSpan;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.TextView.BufferType;

import com.sticktogether.navigation.Child;
import com.sticktogether.navigation.ExpandableListAdapter;
import com.sticktogether.navigation.Group;

public class NavigationActivity extends Activity {

	public static 	ArrayList<ArrayList<String>> nav_Info; // not a nice solution, but it work for now
	public static 	ArrayList<Drawable> nav_icons;
	private ExpandableListView listView;	// List to show all steps for navigation
	private ExpandableListAdapter adapter;	// Adapter for List controls
	

    private ArrayList<Group> groups = new ArrayList<Group>();
    private ArrayList<ArrayList<Child>> childs = new ArrayList<ArrayList<Child>>();
    
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_navigation);
		
		
		listView = (ExpandableListView) findViewById(R.id.nav_ListView);
	        
        // If a result came from the server
        if(nav_Info != null){
        	fillGroupsAndChilds();
	        adapter = new ExpandableListAdapter(this, groups, childs);
	
	        listView.setAdapter(adapter);
	    }
        else{
        	// no results
        	((TextView)findViewById(R.id.nav_GeneralInfo)).setText("Error: We are sorry! Transit information seems not to be available for your request.");
        }
        
        

        
	}
	
	/**
	 * Get information about the used transport types and their icons.
	 * Create the header for the navigation list
	 */
	private void setHeader(){
		// creating Text for Header
		String lines = "";
		ArrayList<Integer> iconIndex = new ArrayList<Integer>();
		
		iconIndex.add(0);
	
		//Get all transport types and indices for their icons
		for(int i = 0; i < nav_Info.get(1).size(); i++){
			final String tmp = nav_Info.get(1).get(i);
			if(i+1 < nav_Info.get(0).size() ){
				iconIndex.add(iconIndex.get(iconIndex.size()-1)+1+tmp.length());
			}
			lines += " " + tmp;
		}
		
		//add time information
		lines += "\n" + nav_Info.get(2).get(0)+ "\n"; // duration
		lines += nav_Info.get(2).get(1);	// departure time
		//sometimes the arrival time is not mentioned in server response
		if(nav_Info.get(2).size() == 3){
			lines += " - " + nav_Info.get(2).get(2); // arrival time
		}
		
		
        TextView tv  = (TextView)findViewById(R.id.nav_GeneralInfo);
        TextView tv1 =  (TextView)findViewById(R.id.nav_Title);

		//making it possible to show bitmaps within the textview
    	SpannableStringBuilder ssb = new SpannableStringBuilder( lines );
    	
    	for(int i = 0; i < iconIndex.size(); i++){
    		
    		ssb.setSpan( new ImageSpan(this, ((BitmapDrawable)nav_icons.get(i)).getBitmap() ), iconIndex.get(i), iconIndex.get(i)+1, Spannable.SPAN_INCLUSIVE_INCLUSIVE );
    	}
    	
    	tv1.setText(nav_Info.get(nav_Info.size()-2).get(0));
    	tv.setText( ssb, BufferType.SPANNABLE );
	}
	
	/**
	 * Get an ArrayList of Childs with help of all String in the ArrayList at position of the current group
	 * @param groupIndex Index of the group
	 * @return List of Childs for one specific group
	 */
	private ArrayList<Child> getChildArray(int groupIndex){
		ArrayList<Child> newList = new ArrayList<Child>();

		for(int i = 1; i < nav_Info.get(groupIndex).size(); i ++){
			newList.add(new Child(nav_Info.get(groupIndex).get(i)));
		}
		
		return newList;
	}
	
	/**
	 * Fill the ExpandableListView
	 */
	private void fillGroupsAndChilds() {
		setHeader();
		Bitmap d;
		final int width = getResources().getDrawable(R.drawable.rail).getIntrinsicWidth();
		final int height = getResources().getDrawable(R.drawable.rail).getIntrinsicHeight();
		//Add groups and children to their lists
		for(int i = 3; i < nav_Info.size()-2; i++){
	    	d = ((BitmapDrawable)nav_icons.get(i-3)).getBitmap();
			Bitmap bitmapOrig = Bitmap.createScaledBitmap(d, width, height, false);
			groups.add(new Group(nav_Info.get(i).get(0),new BitmapDrawable(getResources(),bitmapOrig)));
			if(nav_Info.get(i).size() > 1)
				childs.add(getChildArray(i));
			else
				childs.add(null);
		}
		

		
		// Add legal info as a group (had scrolling problems when it was in a textview)
		groups.add(new Group(nav_Info.get(nav_Info.size()-1).get(0),null));
        childs.add(null);
	}


}
